package com.jn.rxjava.views;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.icu.util.ChineseCalendar;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.jn.rx_java_demo.R;
import com.jn.rxjava.Client.ApiClient;
import com.jn.rxjava.adapter.NewsAdapter;
import com.jn.rxjava.entity.NewsResponse;
import com.jn.rxjava.service.ApiService;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.disposables.CompositeDisposable;
import io.reactivex.rxjava3.schedulers.Schedulers;


public class TodayActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private TextView weiyuTextView;
    private ImageView  headImageView;
    private NewsAdapter newsAdapter;
    private CompositeDisposable compositeDisposable = new CompositeDisposable();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.today_activity);

        recyclerView = findViewById(R.id.recyclerView);
        weiyuTextView = findViewById(R.id.weiyuTextView);

        headImageView = findViewById(R.id.headImageView);

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        newsAdapter = new NewsAdapter();
        recyclerView.setAdapter(newsAdapter);

        loadNews();


        TextView textViewLunarDate = findViewById(R.id.textViewLunarDate);
        TextView textViewLunarDay = findViewById(R.id.textViewLunarDay);
        TextView weiyuTextView = findViewById(R.id.weiyuTextView);
        TextView textViewGregorianDate = findViewById(R.id.textViewGregorianDate);
        TextView textViewGregorianDay = findViewById(R.id.textViewGregorianDay);

        // 获取当前日期和时间
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        SimpleDateFormat dayFormat = new SimpleDateFormat("EEEE", Locale.getDefault());

        String currentGregorianDate = dateFormat.format(new Date());
        String currentGregorianDay = dayFormat.format(new Date());

        // 获取农历日期
        String currentLunarDate = LunarCalendar.getLunarDateString(new Date());
        String currentLunarDay = LunarCalendar.getLunarDay(new Date());

        // 设置TextView的文本
        textViewLunarDate.setText(currentLunarDate);
        textViewLunarDay.setText(currentLunarDay);
        weiyuTextView.setText("weiyuTextView");
        textViewGregorianDate.setText(currentGregorianDate);
        textViewGregorianDay.setText(currentGregorianDay);
    }

    private void loadNews() {
        ApiService apiService = ApiClient.getClient().create(ApiService.class);
        compositeDisposable.add(
                apiService.getTodayNews()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(this::handleResponse, this::handleError)
        );
    }

    private void handleResponse(NewsResponse response) {
        if (response.getCode() == 200) {
            List<String> newsList = response.getResult().getNews();
            newsAdapter.setNewsList(newsList);
            weiyuTextView.setText(response.getResult().getWeiyu());
            Picasso.get().load(response.getResult().getHeadImage()).into(headImageView);
        } else {
            // Handle error case
        }
    }

    private void handleError(Throwable error) {
        // Handle error
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        compositeDisposable.clear();
    }
}